import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.sys.utils.SignUtil;

import java.security.MessageDigest;
import java.text.MessageFormat;
import java.util.Map;
import java.util.Random;
import java.util.TreeMap;

/**
 * @author Administrator
 * @title: SignUtils
 * @projectName JeePlus
 * @description: TODO
 * @date 2019/9/2419:35
 */

public class SignUtils {
    public String getTimestamp() {
        //生成时间戳

        long timestampLong = System.currentTimeMillis();

        String timestampStr = String.valueOf(timestampLong);

        return timestampStr;
    }

    public static void main(String[] args) throws Exception {
        String nonce = new SignUtils().getNonceStr(16);
        String time = new SignUtils().getTimestamp();
        String tick = "LIKLckvwlJT9cWIhEQTwfN3C81AlAbHXAjWAWvHOzcUJYd_cQeLRH32U3mEgjEsBK9HfH6v7Gl-O4GJRPeIN6g";
        String str = MessageFormat.format("jsapi_ticket={0}&noncestr={1}&timestamp={2}&url=http://k26894v027.wicp.vip/qx.html", tick, nonce, time);
        System.out.println(new SignUtils().shaEncode(str));
        System.out.println(nonce);
        System.out.println(time);
    }

    public String getNonceStr(int length) {

        //生成随机字符串

        String str = "zxcvbnmlkjhgfdsaqwertyuiopQWERTYUIOPASDFGHJKLZXCVBNM1234567890";

        Random random = new Random();

        StringBuffer randomStr = new StringBuffer();

        // 设置生成字符串的长度，用于循环

        for (int i = 0; i < length; ++i) {
            //从62个的数字或字母中选择

            int number = random.nextInt(62);

            //将产生的数字通过length次承载到sb中

            randomStr.append(str.charAt(number));
        }

        //将承载的字符转换成字符串

        return randomStr.toString();
    }

    //类似微信接口的签名生成方法
    public String createSign(Map<String, String> params, String privateKey) {
        StringBuilder sb = new StringBuilder();
        // 将参数以参数名的字典升序排序
        Map<String, String> sortParams = new TreeMap<String, String>(params);
        // 遍历排序的字典,并拼接"key=value"格式
        for (Map.Entry<String, String> entry : sortParams.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue().trim();
            if (!StringUtils.isEmpty(value))
                sb.append("&").append(key).append("=").append(value);
        }
        String stringA = sb.toString().replaceFirst("&", "");

        //String stringSignTemp = stringA + "&"+"appkey="+privateKey;
        //将签名使用MD5加密并全部字母变为大写
        // String signValue = Md5Encrypt.md5(stringSignTemp).toUpperCase();
        // System.out.println("stringA+privateKey后MD5加密+转换全部大写生成sign为：       "+signValue);
        return stringA;
    }

    public String shaEncode(String inStr) throws Exception {
        MessageDigest sha = null;
        try {
            sha = MessageDigest.getInstance("SHA");
        } catch (Exception e) {
            System.out.println(e.toString());
            e.printStackTrace();
            return "";
        }

        byte[] byteArray = inStr.getBytes("UTF-8");
        byte[] md5Bytes = sha.digest(byteArray);
        StringBuffer hexValue = new StringBuffer();
        for (int i = 0; i < md5Bytes.length; i++) {
            int val = ((int) md5Bytes[i]) & 0xff;
            if (val < 16) {
                hexValue.append("0");
            }
            hexValue.append(Integer.toHexString(val));
        }
        return hexValue.toString();
    }
}
